package com.example.pet.dao;

import com.hj.pet.model.Shop;
import com.hj.pet.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShopDao {
    @Insert("insert into shop(id,shop_id,shop_name,master_id,detail,create_time,phone,pics)" +
            "value(#{id},#{shop_id},#{shop_name},#{master_id},#{detail},now(),#{phone},#{pics})")
    int save(Shop shop);

    @Update("<script>" +
            "update shop" +
            "        <set>" +
            "            <if test=\"null !=shop_name\">" +
            "                shop_name=#{shop_name}," +
            "            </if>" +
            "            <if test=\"null !=detail\">" +
            "                detail=#{detail}," +
            "            </if>" +
            "            <if test=\"null !=phone\">" +
            "                phone=#{phone}," +
            "            </if>" +
            "            <if test=\"null !=pics\">" +
            "                pics=#{pics}," +
            "            </if>" +
            "        </set>" +
            "        where shop_id=#{shop_id}"
            +"</script>")
    int updateByShopId(Shop shop);

    @Select("select * from shop where shop_id={shop_id}")
    Shop findShopByShopId(String shop_id);

    @Select("select * from shop where shop_name=#{shop_name}")
    List<Shop> findShopByName(String shop_name);

    @Select("select * from shop where master_id=#{master_id}")
    List<Shop> findShopByMaster(String master_id);

    // TODO 多条件查询
    @Select("<script>" +
            "select * from shop" +
            "        <where>" +
            "            <if test=\"null !=shop_id\">" +
            "                and shop_id=#{shop_id}" +
            "            </if>" +
            "            <if test=\"null !=shop_name\">" +
            "                and shop_name like concat('%',#{shop_name},'%')" +
            "            </if>" +
            "            <if test=\"null !=master_id\">" +
            "                and master_id=#{master_id}" +
            "            </if>" +
            "        </where>"
            +"</script>")
    List<Shop> findShop(Shop shop);

    @Delete("delete from shop where shop_id=#{shop_id}")
    int deleteById(String shop_id);

    @Select("select count(*) from user_info where user_account = #{account}")
    int searchByAccount(String account);

    @Select("select count(*) from shop where shop_id=#{shop_id}")
    int searchByShopId(String shop_id);

    @Select("select count(*) from shop where master_id=#{master_id} and shop_name=#{shop_name}")
    int searchByShopNameAndAccount(String master_id,String shop_name);

    @Select("select count(*) from shop where master_id=#{master_id}")
    int searchByMaster(String master_id);

    @Update("<script>" +
            "update user_info" +
            "        <set>" +
            "                <if test=\"null !=user_name\">" +
            "                    user_name=#{user_name}," +
            "                </if>" +
            "                <if test=\"null !=phone\">" +
            "                    phone=#{phone}," +
            "                </if>" +
            "                <if test=\"null !=address\">" +
            "                    address=#{address}," +
            "                </if>" +
            "                <if test=\"null !=favorites\">" +
            "                    favorites=#{favorites}," +
            "                </if>" +
            "                <if test=\"null !=url\">" +
            "                    url=#{url}," +
            "                </if>" +
            "                <if test=\"details\">" +
            "                    details=#{details}," +
            "                </if>" +
            "        </set>" +
            "        where user_account=#{user_account}"
            +"</script>")
    int updateById(UserInfo userInfo);

    @Select("select * from user_info where user_account=#{user_account}")
    UserInfo findUserByAc(String user_account);

}
